package hhm.utils;

public class CheckCalledTimesInPeriod {
	private Icallee callee;

	private int limitTime;

	private int limitCount;
	static private long startMills = System.currentTimeMillis();

	// record the times that be called
	static private int count = 0;

	public CheckCalledTimesInPeriod(int second, int limitCount) {
		this.limitTime = second;
		this.limitCount = limitCount;
		callee = new Callee();
	}

	// Limit the times that be called for a period
	public synchronized void execute() {

		long nowMills = System.currentTimeMillis();

		if (nowMills <= (startMills + limitTime * 1000)) {

			if (count <= limitCount) {
				callee.play();
				count++;

			}

		} else {
			startMills = System.currentTimeMillis();
			count = 0;

		}

	}
}
